const puppeteer = require('puppeteer')
const mongondb = require('mongodb')

// 休眠函数
function sleep(second) {
  return new Promise((resolve,reject)=>{
    setTimeout(()=>{
      resolve('enough sleep~')
    },second)
  })
}
// 站点地址
url = `https://segmentfault.com/channel/frontend`
class Parse{
  constructor(){
    this.page = null
    this.browser = null
    this.dataMessage = {}
  }
  async init(){
    // 构造浏览器对像
    // 显示浏览器
    this.browser = await puppeteer.launch({
      'headless':false
    })
    // 创建页面
    this.page = await this.browser.newPage()
    // 模拟浏览器信息
    const UA = `Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36`
    await Promise.all([
      this.page.setUserAgent(UA),
      // 允许执行js脚本
      this.page.setJavaScriptEnabled(true),
      // 页面视口大小
      this.page.setViewport({width:1100,height:1080})
    ])
    await this.getBook()
    await this.page.close()
  }
  async getBook(){
    // 打开页面
    await this.page.goto(url);
    let page = await this.page
    this.dataMessage = await page.evaluate(()=>{
      var list = [...document.querySelectorAll('.news-list .news__item-info a')];
      return list.map(el =>{
        return {href:el.href.trim(),title:el.innerText}
      })
    })
    console.log(this.dataMessage)
    console.log(this.dataMessage.length)
    
  }
}

let parse = new Parse()
parse.init()